package com.tledu.springboot04.service.impl;

import com.tledu.springboot04.core.exception.CustomException;
import com.tledu.springboot04.domain.User;
import com.tledu.springboot04.mapper.IUserMapper;
import com.tledu.springboot04.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.getUserByUsername(username);
        if (user == null) {
            // 抛异常
            throw new CustomException(500, "账号不存在");
        }
        if (!user.getPassword().equals(password)) {
            throw new CustomException(500, "密码不正确");
        }
        return user;
    }
}
